import { request } from '../../request'
import { getBaseUrl } from '@/utils/env'

/**
 * 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
 */
export function code2Session(code: string) {
  return request.get<ApiModels.weixin.JsCode2JsonResult>(`/weixin/wxopen/onLogin?code=${code}`, {
    custom: {
      auth: false,
    },
  })
}

/** 获取生成的微信二维码url */
export function GenerateWxcodeUrl(path: string, scene: string): string {
  return `${getBaseUrl()}/weixin/wxopen/GenerateWxcode?path=${path}&scene=${scene}`
}

/**
 * 解密用户档案
 */
export function decryptUserProfile(data: ApiModels.weixin.EncryptedDataModel) {
  return request.post<ApiModels.weixin.UserProfile>('/weixin/wxopen/DecryptUserInfo', data, {
    custom: {
      auth: false,
    },
  })
}

/**
 * 解密电话号码
 */
export function decryptPhoneNumber(data: ApiModels.weixin.EncryptedDataModel) {
  return request.post<any>('/weixin/wxopen/DecryptPhoneNumber', data, {
    custom: {
      auth: false,
    },
  })
}

/**
 * 将code换取用户手机号
 */
export function getUserPhoneNumberFromCode(code: string) {
  return request.post<string>(`/weixin/wxopen/GetUserPhoneNumberFromCode?code=${code}`, undefined, {
    custom: {
      auth: false,
    },
  })
}
